package com.uchengwang.ui.cm_ui.login.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.LoginConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.cycle.ui.LoadCycleWebHtmlActivity;
import com.uchengwang.ui.cm_ui.login.bean.WXUserInfoBean;
import com.uchengwang.ui.utils.CheckUtil;
import com.uchengwang.ui.utils.MD5;
import com.uchengwang.ui.utils.MyCountDownTime;
import com.uchengwang.ui.utils.NetwotkUtil;
import com.uchengwang.ui.utils.SPUtils;
import com.uchengwang.ui.utils.UCommonUtil;

/**
 * Created by U城网 on 2017/5/24.
 * 注册界面
 */

public class RegisterOrFindPwActivity extends BaseWithTitleActivity implements View.OnClickListener{
    private int funcType = 0;//1注册 2忘记密码
    //手机号 验证码 设置密码控件
    private EditText phone, code, pw;
    private Button codeBt, nextOrOk;//获取验证码 下一步/确定
    private ImageView showPw;//是否显示密码
    private boolean isShow = false;//设置的密码是明文还是隐藏 true 明文
    private LinearLayout bm_linear;
    private TextView state;//用户协议
    private String phoneS, codeS;
    private MyCountDownTime countDownTime;
    private WXUserInfoBean wxUserInfoBean;//三方登录时 这个包含用户信息

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_register);
        funcType = getIntent().getIntExtra(BaseConfig.TYPE, -1);
        wxUserInfoBean = (WXUserInfoBean) getIntent().getSerializableExtra(BaseConfig.BEAN);

        initView();
    }

    private void initView(){
        phone = (EditText) findViewById(R.id.register_phone);
        code = (EditText) findViewById(R.id.register_code);
        pw = (EditText) findViewById(R.id.register_pw);
        codeBt = (Button) findViewById(R.id.register_codeBt);
        codeBt.setOnClickListener(this);
        showPw = (ImageView) findViewById(R.id.register_showPw);
        showPw.setOnClickListener(this);

        nextOrOk = (Button) findViewById(R.id.register_next);
        nextOrOk.setOnClickListener(this);

        bm_linear = (LinearLayout) findViewById(R.id.register_bm_linear);
        state = (TextView) findViewById(R.id.register_state);
        state.setOnClickListener(this);

        switch (funcType){
            case 1:
                setTitle("注册");
                break;
            case 2:
                setTitle("找回密码");
                pw.setHint("请设置新密码");
                bm_linear.setVisibility(View.GONE);
                nextOrOk.setText("确定");
                break;
        }
    }

    @Override
    public void onClick(View view) {
        phoneS = phone.getText().toString();//手机号

        switch (view.getId()){
            case R.id.register_codeBt://获取验证码
                if (!NetwotkUtil.isConnectInternet()){
                    UCommonUtil.getUtil().showToast(this, "请检查网络");
                    return;
                }

                if (!CheckUtil.getChecker().checkTelNum(phoneS)){
                    UCommonUtil.getUtil().showToast(this, "手机号码格式不正确");
                    return;
                }
                if (countDownTime == null){
                    countDownTime = new MyCountDownTime(60000, 1000, codeBt);
                }
                countDownTime.start();

                switch (funcType){
                    case 1://注册
                        regGetCode();
                        break;
                    case 2://忘记密码
                        findPwGetCode();
                        break;
                }
                break;

            case R.id.register_showPw://是否显示密码
                if (isShow){
                    showPw.setImageResource(R.drawable.hide_pw);
                    pw.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    pw.setSelection(pw.getText().length());
                    isShow = false;
                }else {
                    showPw.setImageResource(R.drawable.show_pw);
                    pw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    pw.setSelection(pw.getText().length());

                    isShow = true;
                }
                break;

            case R.id.register_next:
                codeS = code.getText().toString();//验证码
                if (!CheckUtil.getChecker().checkTelNum(phoneS)){
                    UCommonUtil.getUtil().showToast(this, "手机号码格式不正确");
                    return;
                }
                if (TextUtils.isEmpty(codeS)){
                    UCommonUtil.getUtil().showToast(this, "验证码不能为空");
                    return;
                }
                if (TextUtils.isEmpty(pw.getText().toString())){
                    UCommonUtil.getUtil().showToast(this, "密码不能为空");
                    return;
                }
                //下一步/确定
                switch (funcType){
                    case 1://注册
//                        reg();
                        Intent intent = new Intent(this, CompleteInfoActivity.class);
                        intent.putExtra(BaseConfig.MOBILE, phoneS);
                        intent.putExtra(BaseConfig.CODE, codeS);
                        intent.putExtra(BaseConfig.PASS_WORD, pw.getText().toString());
                        startActivity(intent);

                        break;
                    case 2://忘记密码
                        findPw();
                        break;
                }
                break;

            case R.id.register_state:
                //用户协议
                Intent intent = new Intent(this, LoadCycleWebHtmlActivity.class);
                intent.putExtra(BaseConfig.IS_HIDE_RIGHT, true);
                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/static/agreement.html");
                startActivity(intent);


//                startActivity(new Intent(this, RegisterProtocolActivity.class));
                break;
        }
    }

//    /**
//     * 注册
//     * */
//    private void reg(){
//        final String pwS = pw.getText().toString();//密码
//
//        if (TextUtils.isEmpty(pwS)){
//            UCommonUtil.getUtil().showToast(this, "请设置密码");
//            return;
//        }
//
//        LoadingUtils.getUtils().loading(this);
//        MapRequest map = new MapRequest();
//        map.put(BaseConfig.USER_NAME, phoneS);
//        map.put(BaseConfig.MOBILE, phoneS);
//        map.put(BaseConfig.USER_PWD, MD5.md5(String.valueOf(pwS)));
//        map.put(BaseConfig.OPT_SYSTEM, BaseConfig.system);
//        map.put(BaseConfig.DEVICE_ID, UCommonUtil.getUtil().getDeviceId(this));
//        map.put(BaseConfig.VALID_CODE, codeS);
//        HttpManager.newInstance(this).query(LoginConfig.Register.method, LoginConfig.Register.URL, map, new HttpCallBack() {
//            @Override
//            public void onError(int status, String msg) {
//                LoadingUtils.getUtils().hide();
//                UCommonUtil.getUtil().showToast(RegisterOrFindPwActivity.this, msg);
//            }
//
//            @Override
//            public void onResp(int status, String msg, String data) {
//                LoadingUtils.getUtils().hide();
//                if (status == 0){
//                    //注册成功
//                    ActivityUtils.getUtils().clearMainActivity();
//
//                    UserBean userBean = JsonUtils.getManager().stringToBean(data, UserBean.class);
//                    UChengApplication.TOKEN = userBean.getToken();
//                    UChengApplication.uid = userBean.getId();
//                    UChengApplication.nickName = userBean.getNickName();
//                    UChengApplication.uniId = userBean.getUniId();
//                    UChengApplication.myUniId =userBean.getUniId();
//                    UChengApplication.userRole = userBean.getUserRole();
//
//                    UChengApplication.schoolName = userBean.getUniName();
//                    UChengApplication.mySchoolName = userBean.getUniName();
//
//                    LiteOrmDBUtil.getLiteOrm(RegisterOrFindPwActivity.this);
//
//                    SPUtils.getPreferences(RegisterOrFindPwActivity.this).setAccountId(userBean.getId());
//                    SPUtils.getPreferences(RegisterOrFindPwActivity.this).setAccountPw(MD5.md5(String.valueOf(pwS)));
//                    SPUtils.getPreferences(RegisterOrFindPwActivity.this).setUserName(phoneS);
//                    SPUtils.getPreferences(RegisterOrFindPwActivity.this).setRealName(userBean.getRealName());
//                    SPUtils.getPreferences(RegisterOrFindPwActivity.this).putLong(BaseConfig.UNI_ID, userBean.getUniId());
//                    SPUtils.getPreferences(RegisterOrFindPwActivity.this).putString(BaseConfig.UNI_NAME, userBean.getUniName());
//                    SPUtils.getPreferences(RegisterOrFindPwActivity.this).setToken(userBean.getToken());
//                    SPUtils.getPreferences(RegisterOrFindPwActivity.this).setTokenTime();
//                    SPUtils.getPreferences(RegisterOrFindPwActivity.this).setLinkMainToken(userBean.getToken());
//                    SPUtils.getPreferences(RegisterOrFindPwActivity.this).putInt(BaseConfig.USER_ROLE, userBean.getUserRole());
//
//                    toNext();
//                }else {
//                    UCommonUtil.getUtil().showToast(RegisterOrFindPwActivity.this, "注册失败,请准确填写信息");
//                }
//            }
//        });
//    }

    /**
     * 找回密码
     * */
    private void findPw(){
        final String pwS = pw.getText().toString();//密码

        if (TextUtils.isEmpty(pwS)){
            UCommonUtil.getUtil().showToast(this, "请设置新密码");
            return;
        }
        MapRequest map = new MapRequest();
        map.put(BaseConfig.USER_NAME, phoneS);
        map.put(BaseConfig.USER_PWD, MD5.md5(String.valueOf(pwS)));
        map.put(BaseConfig.OPT_SYSTEM, BaseConfig.system);
        map.put(BaseConfig.DEVICE_ID, "");
        map.put(BaseConfig.VALID_CODE, codeS);
        map.put(BaseConfig.MOBILE, phoneS);
        HttpManager.newInstance(this).query(LoginConfig.ChangePw.method, LoginConfig.ChangePw.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                UCommonUtil.getUtil().showToast(RegisterOrFindPwActivity.this, msg);
            }
            @Override
            public void onResp(int status, String msg, String data) {
                if (status == 0){
                    //重置成功
                    UCommonUtil.getUtil().showToast(RegisterOrFindPwActivity.this, "修改成功");

                    SPUtils.getPreferences(RegisterOrFindPwActivity.this).setAccountPw(MD5.md5(String.valueOf(pwS)));
                    SPUtils.getPreferences(RegisterOrFindPwActivity.this).setUserName(phoneS);

                    startActivity(new Intent(RegisterOrFindPwActivity.this, LoginActivity.class));
                    RegisterOrFindPwActivity.this.finish();
                }else {
                    UCommonUtil.getUtil().showToast(RegisterOrFindPwActivity.this, "修改失败,请准确填写信息");
                }
            }
        });
    }

    /**
     * 注册-获取验证码
     * */
    private void regGetCode(){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.MOBILE, phoneS);
        HttpManager.newInstance(this).query(LoginConfig.GetCodeRegister.method, LoginConfig.GetCodeRegister.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                countDownTime.cancel();
                countDownTime.onFinish();
            }
            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                if (status == 0){
                    //注册成功
                    UCommonUtil.getUtil().showToast(RegisterOrFindPwActivity.this, "验证码发送成功");
                }else {
                    UCommonUtil.getUtil().showToast(RegisterOrFindPwActivity.this, msg);
                    countDownTime.cancel();
                    countDownTime.onFinish();
                }
            }
        });
    }

    /**
     * 找回密码-获取验证码
     * */
    private void findPwGetCode(){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.MOBILE, phoneS);

        HttpManager.newInstance(this).query(LoginConfig.GetCodeChangePw.method, LoginConfig.GetCodeChangePw.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                countDownTime.cancel();
                countDownTime.onFinish();
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                if (status == 0){
                    //注册成功
                    UCommonUtil.getUtil().showToast(RegisterOrFindPwActivity.this, "验证码发送成功");
                }else {
                    UCommonUtil.getUtil().showToast(RegisterOrFindPwActivity.this, msg);
                    countDownTime.cancel();
                    countDownTime.onFinish();
                }
            }
        });
    }

    private void toNext(){
        if (funcType ==1){
            Intent intent = new Intent(this, CompleteInfoActivity.class);
            if (wxUserInfoBean != null){
                intent.putExtra(BaseConfig.BEAN, wxUserInfoBean);
            }
            startActivity(intent);
        }
        this.finish();
    }
}
